namespace Admin.Domain.Entities;

public class Department : EntityBase
{
    public Department()
    {
        
    }
    /// <summary>
    /// 部门名称
    /// </summary>
    public string DepartmentName { get; set; } = null!;

    /// <summary>
    /// 上级部门Id（根部门为null）
    /// </summary>
    public Guid? ParentId { get; set; }

    /// <summary>
    /// 子部门集合
    /// </summary>
    public virtual ICollection<Department> Children { get; set; } = new HashSet<Department>();

    /// <summary>
    /// 部门下的用户集合
    /// </summary>
    public virtual ICollection<User> Users { get; set; } = new HashSet<User>();

    /// <summary>
    /// 部门负责人Id
    /// </summary>
    public Guid? ManagerId { get; set; }

    /// <summary>
    /// 私有的构造函数
    /// </summary>
    private Department(string departmentName)
    {
        DepartmentName = departmentName;
    }

    /// <summary>
    /// 注册新部门
    /// </summary>
    /// <param name="departmentName">部门名</param>
    /// <returns>部门对象 或 null</returns>
    public static Department? CreateDepartment(string departmentName)
    {
        if (!ValidateDepartmentName(departmentName))
        {
            return null;
        }

        return new Department(departmentName);
    }

    /// <summary>
    /// 软删除部门
    /// </summary>
    /// <returns>是否删除成功</returns>
    public bool DeleteDepartment()
    {
        if (IsDeleted)
        {
            return false;
        }
        IsDeleted = true;
        return true;
    }

    /// <summary>
    /// 更新部门信息
    /// </summary>
    /// <param name="newDepartmentName">新的部门名</param>
    /// <param name="newDescription">新的简介</param>
    /// <returns>是否修改成功</returns>
    public bool UpdateDepartmentInfo(string newDepartmentName, string? newDescription)
    {
        // 验证新的部门名是否有效
        if (!ValidateDepartmentName(newDepartmentName))
        {
            return false;
        }

        // 检查部门是否已经被软删除
        if (IsDeleted)
        {
            return false;
        }

        // 更新部门名
        DepartmentName = newDepartmentName;
        Description = newDescription;
        // 返回更新成功
        return true;
    }

    /// <summary>
    /// 更新部门的上级部门
    /// </summary>
    /// <param name="newParentId">新的部门Id</param>
    /// <returns>是否修改成功</returns>
    public bool UpdateParentId(Guid? newParentId)
    {
        // 检查部门是否已经被软删除
        if (IsDeleted)
        {
            return false;
        }

        // 更新上级部门ID
        ParentId = newParentId;
        UpdatedAt = DateTime.UtcNow;
        // 返回更新成功
        return true;
    }

    /// <summary>
    /// 添加部门到子部门集合
    /// </summary>
    /// <param name="department">被添加的子部门</param>
    /// <returns>是否修改成功</returns>
    public bool AddChildrenDepartment(Department department)
    {
        // 检查部门是否已经被软删除
        if (IsDeleted)
        {
            return false;
        }

        Children.Add(department);
        return true;
    }

    /// <summary>
    /// 移除部门出子部门集合
    /// </summary>
    /// <param name="department">被移除的子部门</param>
    /// <returns>是否修改成功</returns>
    public bool RemoveChildrenDepartment(Department department)
    {
        // 检查部门是否已经被软删除
        if (IsDeleted)
        {
            return false;
        }

        Children.Remove(department);
        return true;
    }

    /// <summary>
    /// 添加用户到用户集合
    /// </summary>
    /// <param name="user">被添加的用户</param>
    /// <returns>是否修改成功</returns>
    public bool AddUser(User user)
    {
        // 检查部门是否已经被软删除
        if (IsDeleted)
        {
            return false;
        }

        Users.Add(user);
        return true;
    }

    /// <summary>
    /// 移除用户出用户集合
    /// </summary>
    /// <param name="department">被移除的用户</param>
    /// <returns>是否修改成功</returns>
    public bool RemoveUser(User user)
    {
        // 检查部门是否已经被软删除
        if (IsDeleted)
        {
            return false;
        }

        Users.Remove(user);
        return true;
    }

    /// <summary>
    /// 更新部门的负责人ID
    /// </summary>
    /// <param name="newManagerId">新的负责人ID</param>
    /// <returns>是否修改成功</returns>
    public bool UpdateManagerId(Guid? newManagerId)
    {
        // 检查部门是否已经被软删除
        if (IsDeleted)
        {
            return false;
        }

        // 更新负责人ID
        ManagerId = newManagerId;
        // 返回更新成功
        return true;
    }

    /// <summary>
    /// 启用部门
    /// </summary>
    /// <returns>是否启用成功</returns>
    public bool EnableRole()
    {
        // 检查部门是否已经被软删除
        if (IsDeleted)
        {
            return false;
        }

        // 检查部门是否已经被启用
        if (IsActive)
        {
            return false;
        }

        IsActive = true;

        // 返回成功
        return true;
    }

    /// <summary>
    /// 禁用部门
    /// </summary>
    /// <returns>是否禁用成功</returns>
    public bool DisableRole()
    {
        // 检查部门是否已经被软删除
        if (IsDeleted)
        {
            return false;
        }

        // 检查部门是否已经被禁用
        if (!IsActive)
        {
            return false;
        }

        IsActive = false;

        // 返回成功
        return true;
    }

    /// <summary>
    /// 验证部门名
    /// </summary>
    /// <param name="departmentName">部门名</param>
    private static bool ValidateDepartmentName(string departmentName)
    {
        // 验证部门名是否为空
        if (string.IsNullOrWhiteSpace(departmentName))
        {
            return false;
        }

        // 可以添加其他验证

        return true;
    }
}